using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShipExplorerList : MonoBehaviour
{

    public CallbackShip m_OnShipSelectedCallback;
    public GameObject m_ShipExplorerContentParent;
    public ShipExplorerItem m_ItemPrefab;
    public List<ShipExplorerItem> m_Items = new List<ShipExplorerItem>();
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Init()
    {
        ClearItems();
        int numberOfShips = ShipsManager.Instance.GetNumberOfPrefabShips();
        for (int i=0; i< numberOfShips; ++i)
        {
            var ship = ShipsManager.Instance.GetPrefabShip(i);

            AddItem(ship);
        }
        
    }

    public void AddItem(Ship ship)
    {
        var item = Instantiate(m_ItemPrefab);
        item.Init(ship);
        item.transform.SetParent(transform);
        item.m_ExplorerList = this;
        m_Items.Add(item);
    }

    void ClearItems()
    {
        for(int i=0; i<m_Items.Count; ++i)
        {
            Destroy(m_Items[i].gameObject);
        }
        m_Items.Clear();
    }
    public void OnShipItemOpenByBlueprintClicked(Ship ship)
    {
        if (m_OnShipSelectedCallback != null)
            m_OnShipSelectedCallback(ship);
    }
    public void OnShipItemDeleteClicked(Ship ship)
    {
        for(int i=0; i< m_Items.Count; ++i)
        {
            if(m_Items[i].m_Ship == ship)
            {
                Destroy(m_Items[i].gameObject);
                ShipsManager.Instance.RemovePrefabShip(m_Items[i].m_Ship);
                break;
            }    

        }
    }
}
